#include "StdAfx.h"
#include "Resource.h"
#include "NextElementPane.h"
#include "LocalPane.h"
#include "Utility.h"
#include "Element.h"


CNextElementPane::CNextElementPane(CLocalPane* pSquarePane, CWnd* pWnd)
    : m_pSquarePane(pSquarePane)
    , m_pPaneWnd(pWnd)
    , m_pNextElement(NULL)
{
    Init();
}

CNextElementPane::~CNextElementPane()
{
}

void CNextElementPane::DrawNextElement(bool bGetNext)
{
    // Draw background
    DrawBackground();

    // Draw element

    CPoint pntStart(0,0);
    if (bGetNext || NULL == m_pNextElement)
    {
        if (NULL != m_pNextElement)
        {
            delete m_pNextElement;
            m_pNextElement = NULL;
        }

        ElementAttr attr = m_pSquarePane->GetNextElementAttr();
        m_pNextElement = m_pSquarePane->ConstructElement(pntStart, attr);

        ASSERT(NULL != m_pNextElement);

        m_pNextElement->SetSquareWidth(m_nSquareWidth);

        CRect rectElement = m_pNextElement->GetOutlineRect();

        pntStart.x = m_rectPane.left + (m_rectPane.Width() - rectElement.Width()) / 2;
        pntStart.y = m_rectPane.top + (m_rectPane.Height() - rectElement.Height()) / 2;

        m_pNextElement->SetFirstPoint(pntStart);

    }

    m_pNextElement->Fill(m_pPaneDC);
    
}

void CNextElementPane::Init()
{
    // Calculate square width of next element
    m_pPaneWnd->GetClientRect(&m_rectPane);
    m_nSquareWidth = CalcSquareWidth(&m_rectPane, 5, 5);
    m_pPaneDC = m_pPaneWnd->GetDC();
}
    
void CNextElementPane::DrawBackground()
{
    CBrush brush, *pOldBrush;
	brush.CreateSolidBrush(RGB(0,0,0));
	pOldBrush = m_pPaneDC->SelectObject(&brush);

	m_pPaneDC->FillRect(&m_rectPane, &brush);
	m_pPaneDC->SelectObject(pOldBrush); 
}

void CNextElementPane::Restart()
{
    delete m_pNextElement;
    m_pNextElement = NULL;   
}